package com.xhwl.centiotdata.suppliersystem.haikangyunmou;

import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouDevice;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/14/ 09:00
 */
@Slf4j
@Service
public class DeviceHaiKangYunMouHandler {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    public IDeviceCentService deviceCentService;
    @Autowired
    private HaiKangYunMouUtils haiKangYunMouUtils;

    // 获取门禁
    public List<HaiKangYunMouDevice> pullDoor(DeviceIntegrationConfigDTO configDTO) {
        List<HaiKangYunMouDevice> deviceList = getDeviceList(configDTO);
        if (!CollectionUtils.isEmpty(deviceList)) {
            haiKangYunMouUtils.mapThirdDeviceId(configDTO.getProjectId(), configDTO.getProductId(), deviceList);
        }
        return deviceList;
    }

    // 获取门禁在离线状态
    public List<HaiKangYunMouDevice> pullDoorOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        List<HaiKangYunMouDevice> deviceList = getDeviceList(configDTO);
        if (!CollectionUtils.isEmpty(deviceList)) {
            haiKangYunMouUtils.mapThirdDeviceId(configDTO.getProjectId(), configDTO.getProductId(), deviceList);
        }
        return deviceList;
    }

    /**
     * 拉取海康云眸设备
     * @param configDTO
     * @return
     */
    private List<HaiKangYunMouDevice> getDeviceList(DeviceIntegrationConfigDTO configDTO) {
        List<HaiKangYunMouDevice> devices = new ArrayList<>();
        Integer projectId = configDTO.getProjectId();
        PedestrianIntegrationConfigDTO config = haiKangYunMouUtils.getConfig(projectId);
        if (ObjectUtils.isEmpty(config)) {
            throw new RuntimeException("获取人行配置异常");
        }
        List<String> communityIdStr = haiKangYunMouUtils.getCommunityId(config);
        if (StringUtils.isEmpty(communityIdStr)) {
            throw new RuntimeException("获取社区id失败");
        }
        //新增一个项目对应多个社区ID
        communityIdStr.forEach(communityId ->{
            List<HaiKangYunMouDevice> commDevices = haiKangYunMouUtils.getDevices(config, communityId);
            if (!CollectionUtils.isEmpty(commDevices)){
                devices.addAll(commDevices);
            }
        });

        if (CollectionUtils.isEmpty(devices)) {
            return null;
        }
        return devices;
    }


    /**
     * 获取完整人行配置
     */
    // public PedestrianIntegrationConfigDTO getConfig(String projectIdKey) {
    //     PedestrianIntegrationConfigDTO config;
    //     Object o = redisUtils.get(projectIdKey);
    //     if (ObjectUtils.isEmpty(o)) {
    //         setConfig();
    //         config = (PedestrianIntegrationConfigDTO) redisUtils.get(projectIdKey);
    //     } else {
    //         config = (PedestrianIntegrationConfigDTO) o;
    //     }
    //     return config;
    // }

    /**
     * 设置完整人行配置
     */
    // public void setConfig() {
    //     // 获取到需要执行拉取人行出入记录的项目及配置
    //     List<PedestrianIntegrationConfigDTO> integrationConfigDTOList = integrationConfig.getIntegrationConfig(null)
    //             .getOrDefault(PedestrianMethodEnum.DEVICE.getId(), Collections.emptyList());
    //     integrationConfigDTOList.forEach(config -> {
    //         try {
    //             String projectIdKey = config.getProjectId() + ":" + config.getProductId() + ":" + HaiKangYunMouConstant.PROJECT_ID;
    //             redisUtils.setWithMinute(projectIdKey, config, 30);
    //         } catch (Exception e) {
    //             e.printStackTrace();
    //         }
    //     });
    // }

    /**
     * 获取社区id
     *
     */
    // public String getCommunityId(PedestrianIntegrationConfigDTO config) {
    //     Integer projectId = config.getProjectId();
    //     String orgIndexCode = config.getOrgIndexCode();
    //     String orgStr = JSONObject.toJSONString(orgIndexCode);
    //     Object obj = JSON.parse(orgStr);
    //     String objStr = obj.toString();
    //     List<HaiKangYunMouProject> list = JSONObject.parseArray(objStr, HaiKangYunMouProject.class);
    //     Map<Integer, String> map = list.stream().collect(
    //             Collectors.toMap(HaiKangYunMouProject::getProjectId, HaiKangYunMouProject::getCommunityId));
    //     return map.get(projectId);
    // }


}
